package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;

import android.view.View;

import androidx.viewpager2.widget.ViewPager2;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.util.ImageUtil;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.migu.contacts.R;
import com.migu.contacts.bean.ContactBean;
import com.migu.contacts.bean.GroupBean;
import com.migu.contacts.databinding.ActivityRenMaiBinding;
import com.migu.contacts.databinding.ItemRenmaiAllBinding;
import com.migu.contacts.databinding.ItemRenmaiDetailBinding;
import com.migu.contacts.databinding.ItemRenmainGroupBinding;
import com.migu.contacts.util.GroupUtil;
import com.migu.contacts.util.UserCenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class RenMaiActivity extends BaseActivity<ActivityRenMaiBinding> {
    @BindAdapter(R.id.recyclerView)
    public BaseAdapter<ItemRenmaiAllBinding, Bean> baseAdapter = new BaseAdapter<ItemRenmaiAllBinding, Bean>() {
        @Override
        protected void onBindData(ItemRenmaiAllBinding binding, Bean bean, int position) {
            super.onBindData(binding, bean, position);
            FlexboxLayoutManager layoutManager = (FlexboxLayoutManager) binding.recyclerView.getLayoutManager();
            layoutManager.setFlexDirection(FlexDirection.ROW);
            BaseAdapter<ItemRenmaiDetailBinding, ContactBean> adapter = new BaseAdapter<ItemRenmaiDetailBinding, ContactBean>() {
            };
            binding.recyclerView.setAdapter(adapter);
            adapter.setData(bean.list);
        }
    };


    @Override
    protected void initData() {
        super.initData();
        API.queryGroup().watch(this, new HttpCallback<List<GroupBean>>() {
            @Override
            protected void onSuccess(List<GroupBean> groupBeans) {
                GroupUtil.setGroups(groupBeans);
            }
        });
        API.contactsAll().watch(this, new HttpCallback<List<ContactBean>>() {
            @Override
            protected void onSuccess(List<ContactBean> contactBeans) {
                if (contactBeans == null) {
                    return;
                }
                Map<Integer, List<ContactBean>> map = new HashMap<>();
                for (ContactBean contactBean : contactBeans) {
                    if (contactBean.id == UserCenter.userBean.id) {
                        continue;
                    }
                    List<Integer> groupIdList = contactBean.groupIdList;
                    if (groupIdList == null || groupIdList.isEmpty()) {
                        continue;
                    }
                    for (Integer i : groupIdList) {
                        map.computeIfAbsent(i, new Function<Integer, List<ContactBean>>() {
                            @Override
                            public List<ContactBean> apply(Integer integer) {
                                return new ArrayList<>();
                            }
                        }).add(contactBean);
                    }
                }
                list.clear();
                for (Map.Entry<Integer, List<ContactBean>> entry : map.entrySet()) {
                    Integer key = entry.getKey();
                    List<ContactBean> value = entry.getValue();
                    String groupName = GroupUtil.getGroupName(key);
                    Bean bean = new Bean();
                    bean.name = groupName;
                    bean.id = key;
                    bean.icon = GroupUtil.getGroupBean(key).icon;
                    bean.list = value;
                    list.add(bean);
                }
                init();
            }
        });
    }

    private void init() {
        if (list.isEmpty()) {
            mBinding.emptyView.setVisibility(View.VISIBLE);
            mBinding.viewpager.setVisibility(View.GONE);
        } else {
            List<List<Bean>> lists = new ArrayList<>();
            for (int i = 0; i < list.size(); i += 5) {
                lists.add(list.subList(i, Math.min(i + 5, list.size())));
            }
            mBinding.emptyView.setVisibility(View.GONE);
            mBinding.viewpager.setVisibility(View.VISIBLE);
            BaseAdapter<ItemRenmainGroupBinding, List<Bean>> adapter = new BaseAdapter<ItemRenmainGroupBinding, List<Bean>>() {
                @Override
                protected void onBindData(ItemRenmainGroupBinding binding, List<Bean> bean, int position) {
                    if (bean.size() > 0) {
                        Bean bean1 = bean.get(0);
                        binding.name1.setText(bean1.name);
                        ImageUtil.loadUrl(binding.bg1, bean1.icon);
                        binding.name1.setVisibility(View.VISIBLE);
                        binding.bg1.setVisibility(View.VISIBLE);
                    } else {
                        binding.name1.setVisibility(View.GONE);
                        binding.bg1.setVisibility(View.GONE);
                    }
                    if (bean.size() > 1) {
                        Bean bean1 = bean.get(1);
                        binding.name2.setText(bean1.name);
                        ImageUtil.loadUrl(binding.bg2, bean1.icon);
                        binding.name2.setVisibility(View.VISIBLE);
                        binding.bg2.setVisibility(View.VISIBLE);
                    } else {
                        binding.name2.setVisibility(View.GONE);
                        binding.bg2.setVisibility(View.GONE);
                    }
                    if (bean.size() > 2) {
                        Bean bean1 = bean.get(2);
                        binding.name3.setText(bean1.name);
                        ImageUtil.loadUrl(binding.bg3, bean1.icon);
                        binding.name3.setVisibility(View.VISIBLE);
                        binding.bg3.setVisibility(View.VISIBLE);
                    } else {
                        binding.name3.setVisibility(View.GONE);
                        binding.bg3.setVisibility(View.GONE);
                    }
                    if (bean.size() > 3) {
                        Bean bean1 = bean.get(3);
                        binding.name4.setText(bean1.name);
                        ImageUtil.loadUrl(binding.bg4, bean1.icon);
                        binding.name4.setVisibility(View.VISIBLE);
                        binding.bg4.setVisibility(View.VISIBLE);
                    } else {
                        binding.name4.setVisibility(View.GONE);
                        binding.bg4.setVisibility(View.GONE);
                    }
                }
            };
            mBinding.viewpager.setAdapter(adapter);
            adapter.setData(lists);
            mBinding.viewpager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
                public void onPageSelected(int position) {
                    List<Bean> list1 = lists.get(position);
                    baseAdapter.setData(list1);
                }
            });
            List<Bean> list1 = lists.get(0);
            baseAdapter.setData(list1);

        }
    }

    List<Bean> list = new ArrayList<>();

    public static class Bean {
        public String name;
        public int id;
        public String icon;
        public List<ContactBean> list;

        public String getName4Show() {
            return name + "(" + list.size() + ")";
        }
    }
}